package com.xy.blog.user.dao.impl;

import com.github.pagehelper.PageHelper;
import com.xy.blog.core.dao.IBaseCommonDao;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.controller.model.req.query.UserQuery;
import com.xy.blog.user.dao.IUserDao;
import com.xy.blog.user.dao.mapper.UserMapper;
import com.xy.blog.user.dao.model.UserDO;
import com.xy.blog.user.exception.UserException;
import com.xy.blog.user.exception.constants.UserErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/4
 */
@Repository
public class UserDaoImpl implements IUserDao, IBaseCommonDao<UserDO, UserQuery> {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResp<UserDO> listByPage(PageReq<UserQuery> req) {
        UserQuery params = req.getParams();

        PageHelper.startPage(req);
        List<UserDO> users = userMapper.selectAll();
        return PageResp.data(users);
    }

    @Override
    public List<UserDO> select(UserDO item) {
        return null;
    }

    @Override
    public int selectCount(UserDO item) {
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(UserDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            item.setCreateTime(LocalDateTime.now());
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(LocalDateTime.now());
        }
        UserDO query = new UserDO();
        query.setUserCode(item.getUserCode());
        if (userMapper.selectCount(query) > 0) {
            throw new UserException(UserErrorCode.USER_EXITS);
        }
        userMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UserDO getById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(UserDO item) {
        UserDO query = new UserDO();
        query.setUserCode(item.getUserCode());
        if (userMapper.selectCount(query) > 0) {
            throw new UserException(UserErrorCode.USER_EXITS);
        }
        userMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public List<UserDO> listByIds(List<Long> ids) {
        Example exec = new Example(UserDO.class);
        Example.Criteria criteria = exec.createCriteria();
        criteria.andIn("id",ids);
        return userMapper.selectByExample(exec);
    }
}
